/*
 * GUI_Esporta.java
 *
 * 
 */
package it.luigiarpino.alt2jsim.gui;

import it.luigiarpino.alt2jsim.core.Grafo;
import it.luigiarpino.alt2jsim.core.ScriviGrafo;
import it.luigiarpino.alt2jsim.utility.FormLayout;
import it.luigiarpino.alt2jsim.utility.TextFieldColor;

import javax.swing.*;

import java.awt.*;
import java.io.File;
import javax.swing.JOptionPane;

/**
 *
 * @author  Luigi Lorenzo Arpino 534/559
 */
public class GuiEsportaToJsim extends JFrame {
    
    /**
	 * 
	 */
	private static final long serialVersionUID = -8338942329147754593L;

    private JLabel l2;
    private TextFieldColor jTextFieldBuffer;
    private JLabel l3;
    private TextFieldColor jTextFieldBanda;
    private JLabel l1;
    private TextFieldColor jTextFieldRitardo;
    private JButton jButtonInvia;
    private JButton jButtonAnnulla;
    private JCheckBox jCheckBoxBuildLink;
    private JCheckBox jCheckBoxBuildNode;
    private boolean buildNode=true;
    private boolean buildLink=true;
    private Grafo g;
    
    
    /** Creates a new instance of GUI_Esporta */
    public GuiEsportaToJsim(Grafo g_) {
        try{ 
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());     
    	} 
    	catch(Exception e) { }
        this.g=g_;
        this.setTitle("Export to Jsim TCL file format");
        
        
        JPanel formPanel = new JPanel(new FormLayout());
        // Riga vuota
	    formPanel.add(new JLabel(" "));
	    formPanel.add(new JLabel(" "));
	    
        // Costruisco la Prima Riga
        this.jCheckBoxBuildLink=new JCheckBox("",true);
        this.jCheckBoxBuildLink.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                BuildLink(evt);
            }
        });
	    formPanel.add(new JLabel("Generate link with delay"));
	    formPanel.add(this.jCheckBoxBuildLink);
	    
        this.jCheckBoxBuildNode=new JCheckBox("",true);
        this.jCheckBoxBuildNode.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                BuildNode(evt);
            }
        });
        formPanel.add(new JLabel("Build node"));
	    formPanel.add(this.jCheckBoxBuildNode);
	    
	    
       
        
        // Costruisco la  Seconda Riga
        this.l1=new JLabel("Max link's delay [second]");
        this.jTextFieldRitardo=new TextFieldColor(8);
        this.jTextFieldRitardo.setText("0");
        formPanel.add(this.l1);
        formPanel.add(this.jTextFieldRitardo);
        
        // Costruisco la  Terza Riga
       
        this.l2 =new JLabel("Node's buffer [Kbytes]");
        this.jTextFieldBuffer=new TextFieldColor(8);
        this.jTextFieldBuffer.setText("1024");
        formPanel.add(this.l2);
        formPanel.add(this.jTextFieldBuffer);
        
        // Costruisco la  Quarta Riga
        
        this.l3=new JLabel("Bandwidth interface nodes [Kbps]");
        this.jTextFieldBanda=new TextFieldColor(8);
        this.jTextFieldBanda.setText("2048");
        formPanel.add(this.l3);
        formPanel.add(this.jTextFieldBanda);
        
        // Costruisco i Bottoni
        this.jButtonInvia=new JButton("Export");
        this.jButtonInvia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonInviaActionPerformed(evt);
            }
        });
        this.jButtonAnnulla=new JButton("Cancel");
        this.jButtonAnnulla.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAnnullaActionPerformed(evt);
            }
        });
        // Riga vuota
	    formPanel.add(new JLabel(" "));
	    formPanel.add(new JLabel(" "));
	    
        formPanel.add(this.jButtonInvia);
        formPanel.add(this.jButtonAnnulla);
        
        this.getContentPane().add(formPanel, BorderLayout.CENTER);
        this.pack();
    }
    
    private void BuildLink(java.awt.event.ItemEvent evt) {
        if (evt.getStateChange() == java.awt.event.ItemEvent.SELECTED){
            this.buildLink=true;
            this.l1.setEnabled(true);
            this.jTextFieldRitardo.setEnabled(true);
        }
        else{
            this.buildLink=false;
            this.l1.setEnabled(false);
            this.jTextFieldRitardo.setEnabled(false);
            this.jTextFieldRitardo.setText("0");
        }
    }
    
    private void BuildNode(java.awt.event.ItemEvent evt) {
        if (evt.getStateChange()==java.awt.event.ItemEvent.SELECTED){
            this.buildNode=true;
            this.l2.setEnabled(true);
            this.l3.setEnabled(true);
            this.jTextFieldBanda.setEnabled(true);
            this.jTextFieldBuffer.setEnabled(true);
        }
        else{
            this.buildNode=false;
            this.l2.setEnabled(false);
            this.l3.setEnabled(false);
            this.jTextFieldBanda.setEnabled(false);
            this.jTextFieldBuffer.setEnabled(false);
            this.jTextFieldBanda.setText("2048");
            this.jTextFieldBuffer.setText("1024");
        }
    }
    
    private void jButtonInviaActionPerformed(java.awt.event.ActionEvent evt) {
        Double ritardo;
        Long buffer;
        Long banda;
        try{
            ritardo=new Double(this.jTextFieldRitardo.getText());
        }
        catch(java.lang.Exception e){
            JOptionPane.showMessageDialog(null,"Link's delay invalid!","Error",JOptionPane.ERROR_MESSAGE);
            return;
        }
        try{
           buffer=new Long(this.jTextFieldBuffer.getText()); 
        }
        catch(java.lang.Exception e){
            JOptionPane.showMessageDialog(null,"Buffer dimension invalid!","Error",JOptionPane.ERROR_MESSAGE);
            return;
        }
        try{
            banda=new Long(this.jTextFieldBanda.getText());
        }
        catch(java.lang.Exception e){
            JOptionPane.showMessageDialog(null,"Link's bandwidth invalid","Error",JOptionPane.ERROR_MESSAGE);
            return;
        }
        JFileChooser f=new JFileChooser();
        f.showDialog(new JFrame(),"Esporta");  
        File esportFile=f.getSelectedFile();
        if (esportFile!=null){
            ScriviGrafo write=new ScriviGrafo(this.g);
            write.scriviFile(esportFile.getAbsolutePath(), ritardo.doubleValue(), buffer.longValue()*1024, banda.longValue()*1024,this.buildLink,this.buildNode);
            this.setVisible(false);
            this.dispose();
        }
    }
    
    
    private void jButtonAnnullaActionPerformed(java.awt.event.ActionEvent evt) {
        this.setVisible(false);
        this.dispose();
    }
    
    
    public static void main(String[] arg)
    {
        GuiEsportaToJsim gui = new GuiEsportaToJsim(null);
        gui.setLocationRelativeTo(null);
        gui.setVisible(true);
    }
    
}
